package com.zhipu.balance.ui.vm.home.system.device

import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.*
import com.zhipu.balance.R
import com.zhipu.balance.mobile.ResultEntity
import com.zhipu.balance.mobile.temporaryClass.DevCode
import com.zhipu.balance.remotedata.InterceptObserver
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.home.system.device.DeviceActivity
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.InitializeUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.*

class DeviceViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService) {

    /**
     * 设备编号
     */
    var mEquipmentNumber = ObservableField(SPUtils.getInstance().getString("DeviceCode"))

    /**
     * 当前IP
     */
    var mCurrentIP = ObservableField(NetworkUtils.getIPAddress(true))

    /**
     * 编号生成
     */
    var NumberGeneration = MutableLiveData<List<String>>()


    /**
     *  版本号
     */
    var mAppVersionName = ObservableField(AppUtils.getAppVersionName())

    /**
     * 获取唯一设备 ID
     */
    var uniqueDeviceId = ObservableField(DeviceUtils.getUniqueDeviceId())


    /**
     * 自动打印
     */
    var mAutomaticPrinting : MutableLiveData<Boolean> = MutableLiveData(SPUtils.getInstance().getBoolean("AutomaticPrinting",false))

    /**
     *  线性定标
     */
    var mLinearScaling : MutableLiveData<Boolean> = MutableLiveData(SPUtils.getInstance().getBoolean("LinearScaling",false))

    /**
     *  不完善细分
     */
    var mImperfectSubdivision : MutableLiveData<Boolean> = MutableLiveData(SPUtils.getInstance().getBoolean("ImperfectSubdivision",false))


    init {
        //编号
        val ftValuesArray: Array<String> = ActivityUtils.getTopActivity().resources.getStringArray(R.array.specimenNumber)
        var dataset = mutableListOf<String>()
        for (SerialNumber in ftValuesArray){
            if(SPUtils.getInstance().getString("SerialNumber").equals(SerialNumber)){
                dataset.add(0,SerialNumber)
            }else{
                dataset.add(SerialNumber)
            }
        }
        NumberGeneration.postValue(dataset)
    }


    /**
     * 获取设备序列号
     */
    fun getDeviceCode(){
        val devCode = DevCode()
        devCode.setKey(DeviceUtils.getAndroidID() + "," + DeviceUtils.getMacAddress().uppercase(Locale.getDefault()))
        devCode.setEndpointType("1")
        //时间戳
        val mTimeStamp = TimeUtils.getNowMills()
        var json = "${mGson.toJson(devCode)}&${mTimeStamp}"
        var observable = mSystemApiService.DeviceCode(toHexString(json),mTimeStamp,devCode)
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : InterceptObserver<String>() {
            /**
             * 成功数据data
             */
            override fun <T> onNextData(resultEntity: ResultEntity<T>?) {
                println(resultEntity?.message.toString())
                SPUtils.getInstance().put("DeviceCode", resultEntity?.message.toString())
                mEquipmentNumber.set(resultEntity?.message.toString())
            }

            /**
             * 错误网络
             */
            override fun onErrorNet(t: Throwable?, netMsg: String?) {
                InitializeUtils.getDefaultMaker()?.show("${t?.message} ${netMsg}")
            }
        })
    }


    /**
     * 主菜单
     */
    fun MainMenu(){
        ActivityUtils.finishActivity(DeviceActivity::class.java)
    }
}